import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/main/lz_mall/model/lz_mall_category_model.dart';
import 'package:shopping_group/main/lz_mall/model/lz_mall_category_prduct_model.dart';
import 'package:shopping_group/main/lz_mall/provider/lz_mall_home_provider.dart';

class LzMallHomeViewModel {
  //请求分类
  getCatogery(BuildContext context, successCallBlock) {
    HttpUtilAll.post(Apis.AUC_MAIN_TREE_URL, success: (result) {
      // 请求成功返回的数据
      List<LzMallCategoryModel> categoryodels = List.from(result).map((e) {
        return LzMallCategoryModel.fromJson(e);
      }).toList();
      successCallBlock(categoryodels);
      //请求第一个分类下的数据
      getCategoryProduct(context, categoryodels.first.id);
    }, error: (errorMsg) {
      // 请求失败返回的错误信息
      BotToast.showText(text: errorMsg);
    });
  }

  //获取该分类的下的数据
  getCategoryProduct(BuildContext context, String code) {
    Map<String, dynamic> data = {};
    data['val'] = code ?? "";
    data['userId'] = BaseConstant.userId;
    data['pageIndex'] = 1;
    data['pageSize'] = 100;

    // BotToast.showLoading();
    HttpUtilAll.post(Apis.CHD_COMMO_SPEC_URL, data: data, success: (result) {
      // 请求成功返回的数据
      List<LzCategoryProductModel> productModels = List.from(result).map((e) {
        return LzCategoryProductModel.fromMap(e);
      }).toList();
      Provider.of<LzMallHomeProvider>(context, listen: false).setDataMap({code: productModels});
    });
  }

  //获取子分类的分类的下的数据
  getSubCategoryProduct(BuildContext context, String code) {
    Map<String, dynamic> data = {};
    data['val'] = code ?? "";
    data['userId'] = BaseConstant.userId;
    data['pageIndex'] = 1;
    data['pageSize'] = "100";

    HttpUtilAll.post(Apis.AUC_COMMO_URL, data: data, success: (result) {
      List<LstCommoSpec> lstCommoSpec = List.from(result).map((e) {
        return LstCommoSpec.fromMap(e);
      }).toList();
      LzCategoryProductModel categoryProductModel = LzCategoryProductModel(id: "-1", lstCommoSpec: lstCommoSpec);

      Provider.of<LzMallHomeProvider>(context, listen: false).setDataMap({
        code: [categoryProductModel]
      });
    }, error: (errorMsg) {
      // 请求失败返回的错误信息
      BotToast.showText(text: errorMsg);
    });
  }

  //获取分类下的数据
  getCategoryAll(String code, Function(List<LstCommoSpec>) successCallBlock) {
    Map<String, dynamic> data = {};
    data['val'] = code ?? "";
    data['pageIndex'] = 1;
    data['pageSize'] = "100";

    HttpUtilAll.post(Apis.AUC_COMMO_URL, data: data, success: (result) {
      List<LstCommoSpec> lstCommoSpec = List.from(result).map((e) {
        return LstCommoSpec.fromMap(e);
      }).toList();

      successCallBlock(lstCommoSpec);
    }, error: (errorMsg) {
      // 请求失败返回的错误信息
      BotToast.showText(text: errorMsg);
    });
  }
}
